package com.dc.controller;

import com.dc.base.controller.BaseController;
import com.dc.base.exception.BusinessException;
import com.dc.base.exception.ResponseCode;
import com.dc.base.pojo.BaseModel;
import com.dc.base.rsa.RSAJS;
import com.dc.pojo.SysLoginLog;
import com.dc.service.LoginService;
import com.wordnik.swagger.annotations.ApiImplicitParam;
import com.wordnik.swagger.annotations.ApiImplicitParams;
import com.wordnik.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.xml.ws.Response;

/**
 * @author Enzo
 * @Description TODO
 * @date 2018-11-12 15:39
 */
@Controller
@ResponseBody
@RequestMapping("index")
public class LoginController extends BaseController {
    @Autowired
    LoginService loginService;

    /**
     * @return com.dc.base.pojo.BaseModel
     * @title:<h3> 用户登录 <h3>
     * @author: Enzo
     * @date: 2018-11-12 15:56
     * @params [loginLog, baseModel]
     **/
    @ApiOperation(value = "登录", notes = "登录", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user_account", value = "帐号"
                    , dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "user_password", value = "密码"
                    , dataType = "String", paramType = "query")
    })
    @RequestMapping("login")
    public BaseModel login(SysLoginLog loginLog, BaseModel baseModel) throws Exception {

        if (loginLog == null) {
            throw new BusinessException(ResponseCode.LOGIN_NULL_USER.getCode(), ResponseCode.LOGIN_NULL_USER.getMessage());
        } else if (loginLog.getUser_account() == null || "".equals(loginLog.getUser_account()) || loginLog.getUser_password() == null || "".equals(loginLog.getUser_password())) {
            throw new BusinessException(ResponseCode.LOGIN_NULL_USER.getCode(), ResponseCode.LOGIN_NULL_USER.getMessage());
        } else {
            loginLog.setType(0);//标志pc登录
            loginLog.setRequest_ip(request.getRemoteAddr());
            loginLog.setVersion_num(request.getHeader("User-Agent"));
            loginLog.setSession_id(session.getId());
            try {
                String password = RSAJS.sessionDecryptHexStr(session, loginLog.getUser_password());
                loginLog.setUser_password(password);
            } catch (Exception e) {
               throw  new BusinessException(ResponseCode.LOGIN_PASSWORD.getCode(),ResponseCode.LOGIN_PASSWORD.getMessage());
            }
            loginService.login(loginLog, baseModel);
            session.setAttribute("login_user", baseModel.getData());
        }
        return baseModel;
    }


    @RequestMapping( value = "/selectEnumRoleMenu",method = RequestMethod.GET)
    @ApiOperation(value = "查看权限菜单", notes = "查看权限菜单", httpMethod = "GET")
    @ResponseBody
    public BaseModel selectEnumRoleMenu(BaseModel baseMode){
        loginService.selectEnumRoleMenu(baseMode);
        baseMode.setMessage("权限查询成功");
        return baseMode;
    }
}
